<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="isEdit ? '修改信息' : '新增信息'"
    width="800px"
    :visible.sync="visible"
  >
    <el-form ref="form" :model="form" label-width="80px" :rules="rules">
      <el-form-item label="姓名" prop="name">
        <el-input
          type="text"
          clearable
          autocomplete="false"
          placeholder="请输入姓名"
          v-model="form.name"
        />
      </el-form-item>
      <el-form-item v-show="false" label="用户ID">
        <el-input type="text" :disabled="false" v-model="form.userId" />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input
          type="text"
          :readOnly="true"
          autocomplete="false"
          placeholder="请输入用户名"
          v-model="form.username"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          type="text"
          clearable
          autocomplete="false"
          placeholder="请输入手机号"
          v-model.number="form.phone"
        />
      </el-form-item>
      <el-form-item label="部门" v-if="type">
        <dept-select ref="deptSelect" :selectValue="form.deptId" @select="handleDeptSelect" />
      </el-form-item>
      <el-form-item label="岗位" v-if="mdl">
        <el-select clearable style="width: 100%" v-model="form.positionId">
          <el-option
            v-for="(item,index) in positionArr"
            :key="index"
            :label="item.positionName"
            :value="item.positionId"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button @click="handleOk" type="primary">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import DeptSelect from '@/components/DeptSelect'
import { updateUserInfo, queryPositionByDeptId, insertUserInfo } from '@/api/department'
import { openNotification, isvalidPhone } from '@/utils/util'

export default {
  components: {
    DeptSelect
  },
  data() {
    const validPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入电话号码'))
      } else if (!isvalidPhone(value)) {
        callback(new Error('请输入正确的11位手机号码'))
      } else {
        callback()
      }
    }
    return {
      isEdit: false,
      visible: false,
      form: {},
      rules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        phone: [{ required: true, validator: validPhone, trigger: 'blur' }]
      },
      positionArr: [],
      type: null,
      mdl: null
    }
  },
  methods: {
    handleDeptSelect(value) {
      this.$set(this.form, 'deptId', value.id)
    },
    handleOk(e) {
      e.stopPropagation()
      this.$refs.form.validate(async valid => {
        if (valid) {
          // this.$set(this.form, 'deptId', this.mdl.parentId)
          let res = null,
            mes = null
          if (this.isEdit) {
            res = await updateUserInfo(this.form)
            mes = '修改操作成功'
          } else {
            res = await insertUserInfo(this.form)
            mes = '新增操作成功'
          }
          console.log('update res', res)
          if (res.state === '000000') {
            openNotification('success', mes)
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    },
    async getId(obj) {
      console.log('obj', obj)
      this.mdl = Object.assign({}, obj)
      const res = await queryPositionByDeptId({ deptId: obj.parentId })
      if (res.state === '000000') {
        if (res.data && res.data.length > 0) {
          this.positionArr = res.data
        } else {
          this.positionArr = []
        }
      }
    },
    edit(bool, row, type = null) {
      this.type = type
      console.log('type', this.type)

      this.isEdit = bool
      console.log('row', row)
      this.form = Object.assign({}, row, this.mdl)
      this.visible = true
    }
  }
}
</script>
